<?php
/**
 * 商户订单导出仓储类
 * User: 13651
 * Time: 2025/9/8 17:31
 */

namespace App\Repository;

use App\Models\OrderExport;

class OrderExportRepo extends CommonRepo
{

    /**
     * 新增导出激励
     * @param array $data
     * @return bool
     */
    public function saveData($data, int $id = 0)
    {
        $result = null;
        if ($id) {
            $result  = OrderExport::query()->find($id);
        } else {
            $result = new OrderExport();
        }
        if (empty($result))
            return 0;
        collect($data)->map(function ($val, $field) use ($result) {
            $result->{$field} = $val;
        });
        return $result->save() ? $result->id : 0;

    }

    /**
     * 获取导出记录
     * @param array $where
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(array $where = [])
    {
        $query = OrderExport::query()->orderBy('created_at', 'desc');

        if (!empty($where['export_channel']))
            $query->where('export_channel', $where['export_channel']);
        if (!empty($where['uid']))
            $query->where('uid', $where['uid']);

        return $query;
    }

    /**
     * 获取待生效数据
     * @param int $status
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getStatusService(int $status=0,int $export_channel = 1)
    {
        return OrderExport::query()->where('status',$status)->where('export_channel',$export_channel)->orderBy('id', 'asc')->limit(3)->get();
    }
}